package org.groovymud.utils;
/* Copyright 2008 Matthew Corby-Eaglen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0 
*
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class CountingMap {

    Map<Object, Integer> backingMap = new HashMap<Object, Integer>();

    public void clear() {
        backingMap.clear();
    }

    public boolean containsKey(Object key) {
        return backingMap.containsKey(key);
    }

    public boolean containsValue(Object value) {
        return backingMap.containsValue(value);
    }

    public Set<?> entrySet() {
        return backingMap.entrySet();
    }

    public boolean equals(Object o) {
        return backingMap.equals(o);
    }

    public Integer get(Object key) {
        return backingMap.get(key);
    }

    public int hashCode() {
        return backingMap.hashCode();
    }

    public boolean isEmpty() {
        return backingMap.isEmpty();
    }

    public Set<?> keySet() {
        return backingMap.keySet();
    }

    public Integer remove(Object key) {
        return backingMap.remove(key);
    }

    public int size() {
        return backingMap.size();
    }

    public Collection<Integer> values() {
        return backingMap.values();
    }

    public Integer put(Object key, Integer value) {
        return backingMap.put(key, value);
    }

    public void putAll(Map<Object, Integer> m) {
        // TODO Auto-generated method stub
        backingMap.putAll(m);
    }

    public void increment(Object key) {
        if (!backingMap.containsKey(key)) {
            backingMap.put(key, new Integer(0));
        } else {
            Integer i = backingMap.get(key);
            backingMap.put(key, new Integer(i.intValue() + 1));
        }
    }

    public void decrement(Object key) {
        if (!backingMap.containsKey(key)) {
            backingMap.put(key, new Integer(0));
        } else {
            Integer i = backingMap.get(key);
            backingMap.put(key, new Integer(i.intValue() - 1));
        }
    }

    public Set getKeysAt(int limit) {
        Set ret = new HashSet<Object>();
        for (Object obj : backingMap.keySet()) {
            if (backingMap.get(obj).intValue() == limit) {
                ret.add(obj);
            }
        }
        return ret;
    }

    public Set getKeysAbove(int limit) {
        Set ret = new HashSet<Object>();
        for (Object obj : backingMap.keySet()) {
            if (backingMap.get(obj).intValue() > limit) {
                ret.add(obj);
            }
        }
        return ret;
    }

    public Set getKeysBelow(int limit) {
        Set ret = new HashSet<Object>();
        for (Object obj : backingMap.keySet()) {
            if (backingMap.get(obj).intValue() < limit) {
                ret.add(obj);
            }
        }
        return ret;
    }

}
